package com.hsc.yalebao.base;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.os.Environment;
import android.os.Looper;
import android.os.Process;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class CrashHandler
        implements Thread.UncaughtExceptionHandler {
    private static CrashHandler INSTANCE = new CrashHandler();
    public static final String TAG = "CrashHandler";
    public static String appName = "yalebao";
    private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
    private Map<String, String> infos = new HashMap();
    private Context mContext;
    private Thread.UncaughtExceptionHandler mDefaultHandler;

    public static CrashHandler getInstance() {
        try {
            CrashHandler localCrashHandler = INSTANCE;
            return localCrashHandler;
        } finally {
            localObject = finally;
            throw localObject;
        }
    }

    private boolean handleException(final Throwable paramThrowable) {
        if (paramThrowable == null)
            return false;
        new Thread() {
            public void run() {
                Looper.prepare();
                CrashHandler.this.collectDeviceInfo(CrashHandler.this.mContext);
                CrashHandler.this.saveCrashInfo2File(paramThrowable);
                Toast.makeText(CrashHandler.this.mContext, "很抱歉，程序出现异常，即将退出。", 1).show();
                Looper.loop();
            }
        }
                .start();
        paramThrowable.printStackTrace();
        return true;
    }

    private String saveCrashInfo2File(Throwable paramThrowable) {
        StringBuffer localStringBuffer = new StringBuffer();
        Iterator localIterator = this.infos.entrySet().iterator();
        while (true) {
            PrintWriter localPrintWriter;
            Throwable localThrowable;
            if (!localIterator.hasNext()) {
                StringWriter localStringWriter = new StringWriter();
                localPrintWriter = new PrintWriter(localStringWriter);
                paramThrowable.printStackTrace(localPrintWriter);
                localThrowable = paramThrowable.getCause();
                if (localThrowable != null)
                    break label334;
                localPrintWriter.close();
                localStringBuffer.append(localStringWriter.toString());
            }
            try {
                long l = System.currentTimeMillis();
                String str1 = this.formatter.format(new Date());
                String str2 = "crash-" + str1 + "-" + l + ".log";
                if (Environment.getExternalStorageState().equals("mounted")) {
                    String str3 = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + appName + "/log/";
                    File localFile = new File(str3);
                    if (!localFile.exists())
                        localFile.mkdirs();
                    FileOutputStream localFileOutputStream = new FileOutputStream(str3 + str2);
                    localFileOutputStream.write(localStringBuffer.toString().getBytes());
                    localFileOutputStream.close();
                }
                return str2;
                Map.Entry localEntry = (Map.Entry) localIterator.next();
                String str4 = (String) localEntry.getKey();
                String str5 = (String) localEntry.getValue();
                localStringBuffer.append(str4 + "=" + str5 + "\n");
                continue;
                label334:
                localThrowable.printStackTrace(localPrintWriter);
                localThrowable = localThrowable.getCause();
            } catch (Exception localException) {
                Log.e("CrashHandler", "an error occured while writing file...", localException);
            }
        }
        return null;
    }

    public void collectDeviceInfo(Context paramContext) {
        while (true) {
            int j;
            Field localField;
            try {
                PackageInfo localPackageInfo = paramContext.getPackageManager().getPackageInfo(paramContext.getPackageName(), 1);
                if (localPackageInfo != null) {
                    if (localPackageInfo.versionName == null) {
                        str1 = "null";
                        String str2 = localPackageInfo.versionCode;
                        this.infos.put("versionName", str1);
                        this.infos.put("versionCode", str2);
                    }
                } else {
                    arrayOfField = Build.class.getDeclaredFields();
                    int i = arrayOfField.length;
                    j = 0;
                    if (j < i)
                        continue;
                    return;
                }
                String str1 = localPackageInfo.versionName;
                continue;
            } catch (PackageManager.NameNotFoundException localNameNotFoundException) {
                Field[] arrayOfField;
                Log.e("CrashHandler", "an error occured when collect package info", localNameNotFoundException);
                continue;
                localField = arrayOfField[j];
            }
            try {
                localField.setAccessible(true);
                this.infos.put(localField.getName(), localField.get(null).toString());
                Log.d("CrashHandler", localField.getName() + " : " + localField.get(null));
                j++;
            } catch (Exception localException) {
                while (true)
                    Log.e("CrashHandler", "an error occured when collect crash info", localException);
            }
        }
    }

    public void init(Context paramContext) {
        this.mContext = paramContext;
        this.mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(this);
    }

    public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
        if ((!handleException(paramThrowable)) && (this.mDefaultHandler != null)) {
            this.mDefaultHandler.uncaughtException(paramThread, paramThrowable);
            return;
        }
        try {
            Thread.sleep(3000L);
            CustomApplication.app.finishAllActivity();
            Process.killProcess(Process.myPid());
            System.exit(0);
            return;
        } catch (InterruptedException localInterruptedException) {
            while (true)
                Log.e("CrashHandler", "error : ", localInterruptedException);
        }
    }
}

/* Location:           C:\Users\user\Desktop\dd28_pcdd720\classes_dex2jar.jar
 * Qualified Name:     com.hsc.yalebao.base.CrashHandler
 * JD-Core Version:    0.6.2
 */